//
//  MasterViewController.m
//  newRefreshTest
//
//  Created by xcq on 15/3/26.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "MJRefresh.h"
@interface MasterViewController ()

@property NSMutableArray *objects;
@end

@implementation MasterViewController

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    [self.tableView  addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshAction) dateKey:@"xixi"];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
      self.navigationItem.rightBarButtonItems= @[addButton];
}

- (void)refreshAction {
    if (self.objects) {
        [self.objects removeAllObjects];
    }else {
        self.objects = [NSMutableArray new];
    }
    for (int i = 0; i < 10 ;i ++) {
        [self.objects addObject:[NSDate date] ];
    }
    [self.tableView reloadData];
    [self.tableView.header endRefreshing];
 }

- (void)footerRefresh {
    if(!self.objects){
        self.objects = [NSMutableArray new];
    }
    for (int i = 0; i < 10; i++) {
        [self.objects insertObject:[NSDate date] atIndex:self.objects.count];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
                    (int64_t)(1.5 * NSEC_PER_SEC)),
                         dispatch_get_main_queue(), ^{
                             [self.tableView reloadData];
                             [self.tableView.footer endRefreshing];
    });
}

- (void)customAction {
    [self.tableView setEditing:!self.tableView.editing animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender {
    if (!self.objects) {
        self.objects = [[NSMutableArray alloc] init];
    }
    [self.objects insertObject:[NSDate date] atIndex:self.objects.count];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}

#pragma mark - Segues

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = self.objects[indexPath.row];
        [[segue destinationViewController] setDetailItem:object];
    }
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDate *object = self.objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

@end
